package com.zyy.smartcity1129;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.JsonObject;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.util.PpUtil;

import okhttp3.RequestBody;

public class LoginActivity extends AppCompatActivity {
    private EditText editTextTextPassword;
    private EditText editTextTextPassword2;
    private Button button;
    private Button button2;
    private TextView textView;
    public static String mToken;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        Http.base=getSharedPreferences("login", Context.MODE_PRIVATE).getString("base","");
        textView.setOnClickListener(v -> {
            PpUtil p = new PpUtil();
            View pv = p.getP(LoginActivity.this, R.layout.p_ip);
            EditText editTextTextPersonName = (EditText) pv.findViewById(R.id.editTextTextPersonName);
            Button button3 = (Button) pv.findViewById(R.id.button3);
            button3.setOnClickListener(V->{
                String ip=editTextTextPersonName.getText().toString().trim();
                if(ip!=null){
                    Http.base="http://"+ip;
                    getSharedPreferences("login", Context.MODE_PRIVATE).edit().putString("base",Http.base);
                    p.closeP();
                }else {
                    Toast.makeText(V.getContext(), "请输入IP地址", Toast.LENGTH_SHORT).show();
                }
            });
        });
        button.setOnClickListener(v->{
            //登录
            String s1=editTextTextPassword.getText().toString();
            String s2=editTextTextPassword2.getText().toString();
            JsonObject object=new JsonObject();
            object.addProperty("username",s1);
            object.addProperty("password",s2);
            new Thread(()->{
                login login=Http.getInstance().get("/prod-api/api/login",RequestBody.create(Http.m,object.toString()),login.class,2);
                runOnUiThread(()->{
                    if(login.token!=null){
                        Http.token=login.token;
                        mToken = login.token;
                        startActivity(new Intent(LoginActivity.this,MainActivity.class));
                    }else {
                        Toast.makeText(LoginActivity.this, login.getMsg(), Toast.LENGTH_SHORT).show();
                    }
                });

            }).start();

        });
    }


    private void initView() {
        editTextTextPassword = (EditText) findViewById(R.id.editTextTextPassword);
        editTextTextPassword2 = (EditText) findViewById(R.id.editTextTextPassword2);
        button = (Button) findViewById(R.id.button);
        button2 = (Button) findViewById(R.id.button2);
        textView = (TextView) findViewById(R.id.textView);

    }
    static class login{

        private String msg;
        private int code;
        private String token;

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getToken() {
            return token;
        }

        public void setToken(String token) {
            this.token = token;
        }
    }
}
